package com.ruoyi.statistics.service.impl;

import com.ruoyi.statistics.domain.Amount;

import com.ruoyi.statistics.domain.OrderChange;
import com.ruoyi.statistics.mapper.SysStatisticsAmountMapper;
import com.ruoyi.statistics.service.ISysStatisticsAmountService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class SysStatisticsAmountServiceImpl implements ISysStatisticsAmountService {
    @Autowired
    private SysStatisticsAmountMapper sysStatisticsAmountMapper;

    @Override
    public List<Amount> getAllStatisticsSpendAmount(Date startTime, Date endTime) {
        List<Amount> result = new ArrayList<>();
        Date currentDate = new Date(startTime.getTime());
        while (!currentDate.after(endTime)) {
            // 查询当前日期的消费金额
            BigDecimal spendAmount = sysStatisticsAmountMapper.getSpendAmountByDate(currentDate);
            if (spendAmount==null){
                spendAmount = BigDecimal.valueOf(0);
            }

            // 构造OrderChange对象
            Amount amount = new Amount();
            amount.setDate(currentDate);
            amount.setAmount(spendAmount);

            result.add(amount);

            // 日期加1天
            currentDate = new Date(currentDate.getTime() + TimeUnit.DAYS.toMillis(1));
        }
        return result;
    }

    @Override
    public List<Amount> getStoreStatisticsSpendAmount(Date startTime, Date endTime, Long deptId) {
        List<Amount> result = new ArrayList<>();
        Date currentDate = new Date(startTime.getTime());
        while (!currentDate.after(endTime)) {
            // 查询当前日期的消费金额
            BigDecimal spendAmount = sysStatisticsAmountMapper.getStoreSpendAmountByDate(currentDate,deptId);
            if (spendAmount==null){
                spendAmount = BigDecimal.valueOf(0);
            }

            // 构造OrderChange对象
            Amount amount = new Amount();
            amount.setDate(currentDate);
            amount.setAmount(spendAmount);

            result.add(amount);

            // 日期加1天
            currentDate = new Date(currentDate.getTime() + TimeUnit.DAYS.toMillis(1));
        }
        return result;
    }

    @Override
    public List<Amount> getAllStatisticsRechargeAmount(Date startTime, Date endTime) {
        List<Amount> result = new ArrayList<>();
        Date currentDate = new Date(startTime.getTime());
        while (!currentDate.after(endTime)) {
            // 查询当前日期的消费金额
            BigDecimal rechargeAmount = sysStatisticsAmountMapper.getRechargeAmountByDate(currentDate);
            if (rechargeAmount==null){
                rechargeAmount = BigDecimal.valueOf(0);
            }

            // 构造OrderChange对象
            Amount amount = new Amount();
            amount.setDate(currentDate);
            amount.setAmount(rechargeAmount);

            result.add(amount);

            // 日期加1天
            currentDate = new Date(currentDate.getTime() + TimeUnit.DAYS.toMillis(1));
        }
        return result;
    }

    @Override
    public List<Amount> getStoreStatisticsRechargeAmount(Date startTime, Date endTime, Long deptId) {
        List<Amount> result = new ArrayList<>();
        Date currentDate = new Date(startTime.getTime());
        while (!currentDate.after(endTime)) {
            // 查询当前日期的消费金额
            BigDecimal rechargeAmount = sysStatisticsAmountMapper.getStoreRechargeAmountByDate(currentDate,deptId);
            if (rechargeAmount==null){
                rechargeAmount = BigDecimal.valueOf(0);
            }

            // 构造OrderChange对象
            Amount amount = new Amount();
            amount.setDate(currentDate);
            amount.setAmount(rechargeAmount);

            result.add(amount);

            // 日期加1天
            currentDate = new Date(currentDate.getTime() + TimeUnit.DAYS.toMillis(1));
        }
        return result;
    }
}
